/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upf.grupoe.p2ppicture.Transfer.Implementations;

import edu.upf.grupoe.p2ppicture.SearchEngine.implementations.InstantMessageImpl;
import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;

/**
 * This class creates objects used to transfer files
 * @author marc
 */
public class TransferImpl {

    private InstantMessageImpl IM = new InstantMessageImpl(null, null);
    private static Log logger = LogFactory.getLog("Main");
    private OutgoingFileTransfer transfer = null;

    public TransferImpl(InstantMessageImpl IM){
        this.IM = IM;
    }
    public OutgoingFileTransfer getOutgoingFT(){
        return this.transfer;
    }
    /**
     * This method sends a file to a destiny
     * @param destiny
     * @param file
     */
    public void sendFile(String destiny, String file){
        FileTransferManager manager = new FileTransferManager(IM.getConnection());
        // Create the outgoing file transfer
        transfer = manager.createOutgoingFileTransfer(destiny);
        try {
            File transferedFile = new File(file);
            if (null != transferedFile) {
                // Send the file
                transfer.sendFile(transferedFile, "Selected Content");
                System.out.println(transfer.getStatus());
            } else {
                logger.error("Error obtaining the desired file");
            }
        } catch (XMPPException ex) {
            logger.error("Error sending the content with FileTransfer"+ex.getMessage());
        }
    }
}